﻿// LSMRegisterList.cs
// Contains a classs for the register list for a Load/Store multiple instruction object

using System;



namespace armsim
{
    /// <summary>
    /// Class containing the register list for a Load/Store multiple instruction
    /// </summary>
    public class LSMRegisterList
    {
        /// <summary>
        /// The uint containing the register flags
        /// </summary>
        public uint RegisterList { get; protected set; }

        /// <summary>
        /// Represents the above Registerlist as an array of bools
        /// </summary>
        public bool[] RegBoolList { get; protected set; }

        /// <summary>
        /// Initializes a new LSMRegisterList
        /// </summary>
        /// <param name="instruction">Initializes from this instruction</param>
        public LSMRegisterList(uint instruction)
        {
            this.RegisterList = VirtualMemory.ExtractBits(instruction, 0, 15);
            this.RegBoolList = new bool[16];

            for (var i = 0; i <= 15; ++i)
            {
                if (VirtualMemory.ExtractBits(RegisterList, i, i) == 1u)
                {
                    RegBoolList[i] = true;
                }
            }

        }

        /// <summary>
        /// Converts the register list to assembly language as a string
        /// </summary>
        /// <param name="cpu">grabs information with this</param>
        /// <returns>the assembly representation</returns>
        public String ToAssembly(VirtualCPU cpu)
        {
            var assembly = String.Empty;
            for (var i = 0; i <= 15; ++i)
            {
                if (VirtualMemory.ExtractBits(RegisterList, i, i) == 1u)
                {
                    assembly += String.Format(" R{0},", i);
                }
            }

            assembly = assembly.Remove(assembly.LastIndexOf(','), 1);
            assembly = "{" + assembly + " }";

            return assembly;
        }

    }
}
